﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class TypeChange : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
                #region   值类型的转换
        //隐式转换
        double d = 10.23f;      //当精度的转换成双精度的，由低类型像高类型转换
        Response.Write(d + "<br>");
        //显示转换
        int a = (int)d;
        Response.Write(a + "<br>");

        string strNumber = "123";
        int ic = int.Parse(strNumber);
        Response.Write(ic + " " + ic.GetType() + "<br>");
        bool bD = int.TryParse("123s", out int _iD);
        Response.Write(bD + "<br>");

        int iE = Convert.ToInt32("12");
        Response.Write(iE + " " + iE.GetType() + "<br>");
        Response.Write(12.3.ToString() + "<br>");

        #endregion

        #region 引用类型的转换
        //隐式转换
        Animal dog = new Dog();
        Animal cock = new Cock();
        dog.Walk();
        dog.Eat();
        cock.Eat();
        cock.Walk();
        //cock.Crow();

        //强制类型转换
        if(cock is Cock)
        {
            ((Cock)cock).Crow();
        }
        Cock _cock = cock as Cock;
        if (_cock != null)
        {
            _cock.Crow();
        }
        #endregion
    }
}
abstract class Animal
{
    public abstract void Walk();
    public void Eat()
    {
        Debug.WriteLine("吃");
    }
}

class Dog : Animal
{
    public override void Walk()
    {
        Debug.WriteLine("四条腿");
    }
}

class Cock : Animal
{
    public override void Walk()
    {
        Debug.WriteLine("两条腿");
    }

    public void Crow()
    {
        Debug.WriteLine("公鸡报晓");
    }
}
